/**
 * Copyright (C) 2025 pony working team
 * SPDX-License-Identifier: MulanPSL-2.0
 */

/* define to prevent recursive inclusion -------------------------------------*/
#ifndef __PONY_W25X_H__
#define __PONY_W25X_H__

/* includes (standard library, system) ---------------------------------------*/
/* includes (other library) --------------------------------------------------*/
/* includes (project) --------------------------------------------------------*/
#include <x_config.h>
#include <hal_mtd.h>
/* includes (local) ----------------------------------------------------------*/

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/* defines -------------------------------------------------------------------*/

#if CONFIG_W25X_SIZE == 128    /* W25Q128: 128bits = 16MB */
#define W25X_NR_PAGES    65536 /* page number */
#define W25X_PAGE_SIZE   256   /* bytes */
#define W25X_NR_SECTORS  4096  /* erasable sectors */
#define W25X_SECTOR_SIZE 4096  /* bytes */
#define W25X_NR_BLOCKS   256   /* erasable blocks */
#define W25X_BLOCK_SIZE  65536 /* bytes */
#else
#error "Unsupported W25X size"
#endif

#define W25X_NR_BYTES (W25X_NR_PAGES * W25X_PAGE_SIZE)

/* Command definitions for W25X flash memory */
#define W25X_WRITE_ENABLE       0x06 /* Write Enable */
#define W25X_WRITE_DISABLE      0x04 /* Write Disable */
#define W25X_READ_STATUS_REG1   0x05 /* Read Status Register-1 */
#define W25X_WRITE_STATUS_REG1  0x01 /* Write Status Register-1 */
#define W25X_READ_STATUS_REG2   0x35 /* Read Status Register-2 */
#define W25X_WRITE_STATUS_REG2  0x31 /* Write Status Register-2 */
#define W25X_READ_STATUS_REG3   0x15 /* Read Status Register-3 */
#define W25X_WRITE_STATUS_REG3  0x11 /* Write Status Register-3 */
#define W25X_READ_DATA          0x03 /* Read Data */
#define W25X_FAST_READ          0x0B /* Fast Read */
#define W25X_PAGE_PROGRAM       0x02 /* Page Program */
#define W25X_SECTOR_ERASE       0x20 /* Sector Erase (4KB) */
#define W25X_BLOCK_ERASE        0xD8 /* Block Erase (64KB) */
#define W25X_CHIP_ERASE         0xC7 /* Chip Erase */
#define W25X_POWER_DOWN         0xB9 /* Power Down */
#define W25X_RELEASE_POWER_DOWN 0xAB /* Release Power Down / Device ID */
#define W25X_MANUFACTURER_ID    0x90 /* Manufacturer/Device ID */
#define W25X_JEDEC_ID           0x9F /* JEDEC ID */
#define W25X_ENABLE_RESET       0x66 /* Enable Reset */
#define W25X_RESET_DEVICE       0x99 /* Reset Device */
#define W25X_DUMMY_BYTE         0xFF

#define W25X_WIP_FLAG 0x01 /* Write In Progress (WIP) flag */

/* typedefs ------------------------------------------------------------------*/
/* macro ---------------------------------------------------------------------*/
/* inlines -------------------------------------------------------------------*/
/* externs -------------------------------------------------------------------*/

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __PONY_W25X_H__ */
